﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DTW_Chart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e)
        {
            Sequence xSeq = Program.sequence1;
            List<PointD> listXSeq = xSeq.getSequence();
            int size = listXSeq.Count;
            PointF[] pointFXSeq = new PointF[size];
            Pen coPen = new Pen(Color.Black,1);
            AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);
            coPen.StartCap = LineCap.RoundAnchor;
            coPen.CustomEndCap = bigArrow;
            int scalingFactor = Program.scalingFactor;

            splitContainer2.Panel1.CreateGraphics().DrawLine(coPen, new Point(10, splitContainer2.Panel1.Height - 10), new Point(10, 10));
            splitContainer2.Panel1.CreateGraphics().DrawLine(coPen, new Point(10, splitContainer2.Panel1.Height - 10), new Point(splitContainer2.Panel1.Width - 10, splitContainer2.Panel1.Height - 10));

            for (int i = 0; i < size; i++)
            {
                pointFXSeq[i] = new PointF(10 + (float)listXSeq[i].X * scalingFactor, splitContainer2.Panel1.Height - (float)listXSeq[i].Y * scalingFactor - 10);
                if (i==0)
                {
                    splitContainer2.Panel1.CreateGraphics().DrawLine(new Pen(Brushes.Black, 1), pointFXSeq[i], pointFXSeq[i]);
                }
                else
                {
                    splitContainer2.Panel1.CreateGraphics().DrawLine(new Pen(Brushes.Black, 1), pointFXSeq[i - 1], pointFXSeq[i]);
                }
            }
        }

        private void splitContainer2_Panel2_Paint(object sender, PaintEventArgs e)
        {
            Sequence ySeq = Program.sequence2;
            List<PointD> listYSeq = ySeq.getSequence();
            int size = listYSeq.Count;
            PointF[] pointFYSeq = new PointF[size];
            Pen coPen = new Pen(Color.Black, 1);
            AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);
            coPen.StartCap = LineCap.RoundAnchor;
            coPen.CustomEndCap = bigArrow;
            int scalingFactor = Program.scalingFactor;

            splitContainer2.Panel2.CreateGraphics().DrawLine(coPen, new Point(10, splitContainer2.Panel2.Height - 10), new Point(10, 10));
            splitContainer2.Panel2.CreateGraphics().DrawLine(coPen, new Point(10, splitContainer2.Panel2.Height - 10), new Point(splitContainer2.Panel2.Width - 10, splitContainer2.Panel2.Height - 10));

            for (int i = 0; i < size; i++)
            {
                pointFYSeq[i] = new PointF(10 + (float)listYSeq[i].X * scalingFactor, splitContainer2.Panel2.Height - (float)listYSeq[i].Y * scalingFactor - 10);
                if (i == 0)
                {
                    splitContainer2.Panel2.CreateGraphics().DrawLine(new Pen(Brushes.Black, 1), pointFYSeq[i], pointFYSeq[i]);
                }
                else
                {
                    splitContainer2.Panel2.CreateGraphics().DrawLine(new Pen(Brushes.Black, 1), pointFYSeq[i - 1], pointFYSeq[i]);
                }
            }
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {
            int scalingFactor = Program.scalingFactor;
            //Redraw Sequence 1
            Sequence xSeq = Program.sequence1;
            List<PointD> listXSeq = xSeq.getSequence();
            int size1 = listXSeq.Count;
            PointF[] pointFXSeq = new PointF[size1];
            Pen coPen1 = new Pen(Color.Black, 1);
            AdjustableArrowCap bigArrow1 = new AdjustableArrowCap(5, 5);
            coPen1.StartCap = LineCap.RoundAnchor;
            coPen1.CustomEndCap = bigArrow1;

            splitContainer1.Panel2.CreateGraphics().DrawLine(coPen1, new Point(10, splitContainer1.Panel2.Height - 10), new Point(10, 10));
            splitContainer1.Panel2.CreateGraphics().DrawLine(coPen1, new Point(10, splitContainer1.Panel2.Height - 10), new Point(splitContainer1.Panel2.Width - 10, splitContainer1.Panel2.Height - 10));

            for (int i = 0; i < size1; i++)
            {
                pointFXSeq[i] = new PointF(10 + (float)listXSeq[i].X * scalingFactor, splitContainer1.Panel2.Height - (float)listXSeq[i].Y * scalingFactor - 10);
                if (i == 0)
                {
                    splitContainer1.Panel2.CreateGraphics().DrawLine(new Pen(Brushes.Blue, 1), pointFXSeq[i], pointFXSeq[i]);
                }
                else
                {
                    splitContainer1.Panel2.CreateGraphics().DrawLine(new Pen(Brushes.Blue, 1), pointFXSeq[i - 1], pointFXSeq[i]);
                }
            }

            //Redraw Sequence 2
            Sequence ySeq = Program.sequence2;
            List<PointD> listYSeq = ySeq.getSequence();
            int size2 = listYSeq.Count;
            PointF[] pointFYSeq = new PointF[size2];
            
            for (int i = 0; i < size2; i++)
            {
                pointFYSeq[i] = new PointF(10 + (float)listYSeq[i].X * scalingFactor, splitContainer1.Panel2.Height - (float)listYSeq[i].Y * scalingFactor - 10);
                if (i == 0)
                {
                    splitContainer1.Panel2.CreateGraphics().DrawLine(new Pen(Brushes.Red, 1), pointFYSeq[i], pointFYSeq[i]);
                }
                else
                {
                    splitContainer1.Panel2.CreateGraphics().DrawLine(new Pen(Brushes.Red, 1), pointFYSeq[i - 1], pointFYSeq[i]);
                }
            }

            //Drawing warping line
            List<MatchingPoints> listMP = Program.resultList;
            int size = listMP.Count;
            Pen warpPen = new Pen(Color.Black, 1);
            AdjustableArrowCap bigArrow2 = new AdjustableArrowCap(3, 3);
            warpPen.CustomStartCap = bigArrow2;
            warpPen.CustomEndCap = bigArrow2;

            for (int i = 0; i < size; i++)
            {
                splitContainer1.Panel2.CreateGraphics().DrawLine(warpPen, 
                    new PointF(10 + (float)listMP[i].first.X * scalingFactor, splitContainer1.Panel2.Height - (float)listMP[i].first.Y * scalingFactor - 10), 
                    new PointF(10 + (float)listMP[i].second.X * scalingFactor, splitContainer1.Panel2.Height - (float)listMP[i].second.Y * scalingFactor - 10));
            }
        }
    }
}
